<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Edit_Profile extends CI_Controller {

    private $data;
	private $countryList; 
	
    public function __construct()
	{
		parent::__construct();
		$this->load->model('system/country_m','country_m',TRUE);
		$this->load->model('system/user_m','user_m',TRUE);
		$this->load->helper('form');
		$this->load->helper('url');		
		
		// Load countries
		$countries = $this->country_m->get_list();
		$options = array( '' => 'Choose country' );
		foreach($countries as $country){
		   $options[$country['id']] = $country['name'];
		}
		$this->countryList = $options;
	}
	public function index()
	{
		if(!empty($_POST)){
             return $this->submit();
		}
		
		$this->data['baseUrl'] =  $this->config->item('base_url');
		$this->data['header'] = 'system/_template/header_v';
		$this->data['content'] = 'system/edit_profile_v';
		$this->data['title'] = 'Edit Profile';
		
		$username = $this->session->userdata(LOGGEDUSERNAME);
		$result = $this->user_m->find_user_by_username($username); 
		if(!$result){
		   // Username not found
			show_404();
		} else { 
		   $this->data['model'] = $result;
		}
		
		$this->data['countries'] = $this->countryList;
		
		try{
			$this->load->view('system/_template/template_v', $this->data);
		}
		catch(Exception $e)
		{
			show_404();
		}
	}
	
	public function submit(){
		if(!empty($_POST)){
			 $this->load->library('form_validation');
			 $this->form_validation->set_message('is_unique','%s is already used');
			 $this->form_validation->set_error_delimiters('<li>', '</li>');
			 // Validation rule check on 'application/config/form_validation.php'
			 
			 try {
			 	if($this->form_validation->run() == TRUE){
				    $username = $this->input->post('username');
					$email = $this->input->post('email');
					$password = $this->config->item('salt') .$this->input->post('password');
					$first_name = $this->input->post('first_name');
					$last_name = $this->input->post('last_name');
					$gender = $this->input->post('gender');
					$phone = $this->input->post('phone');
					$address1 = $this->input->post('address1');
					$address2 = $this->input->post('address2');
					$city = $this->input->post('city');
					$province = $this->input->post('province_state');
					$country = $this->input->post('country');
					$postal_code = $this->input->post('postal_code');
					$facebook = $this->input->post('facebook');
					$twitter = $this->input->post('twitter');
					$website = $this->input->post('website');
					
				    $this->user_m->register( $username, $email, $password, $first_name, $last_name, $gender, $phone, $address1, $address2, $city, $province, $country, $postal_code, $facebook, $twitter, $website);
				
				    $this->data['content'] = 'messages_v';
				    $this->data['msgSuccess'] = "Register success";
				}else{
		            $this->data['content'] = 'system/register_v';
					$this->data['msgError'] = validation_errors();
				}
			 }catch(Exception $e){
				$error = $e->getMessage();
	            $this->data['content'] = 'system/register_v';
				$this->data['msgError'] = "Save Failed - ".$error;
			 }
			 
			 $this->data['baseUrl'] =  $this->config->item('base_url');
 		     $this->data['header'] = 'system/_template/header_anon_v';
		     $this->data['title'] = 'Register';
	 		 $this->data['countries'] = $this->countryList;
		     
			 //$this->data['nextUrl'] = 'Register';
			try{
				$this->load->view('system/_template/template_v', $this->data);
			}
			catch(Exception $e)
			{
				show_404();
			}	
		}
	}
}
?>	